package com.shxy.cloudclassroom.utils;

import com.shxy.cloudclassroom.entity.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import static com.shxy.cloudclassroom.utils.SystemConstant.LOGIN_USER;

/**
 * @Author Happy Pig
 * @School SuiHuaUniversity
 * @Since 1.0
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User)request.getSession().getAttribute(LOGIN_USER);
        if (ObjectUtils.isEmpty(user)) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
